package com.hllxd.hism.star.basic.service.service;

import com.alibaba.fastjson.JSON;
import com.hllxd.hism.star.basic.service.dao.mapper.doris.DorisReportMapper;
import com.hllxd.hism.star.basic.service.dao.mapper.doris.KanbanFlowSummaryCubeMapper;
import com.hllxd.hism.star.basic.service.dao.mapper.mis.ReportMapper;
import com.hllxd.hism.star.basic.service.model.FengkongEntity;
import com.hllxd.hism.star.basic.service.model.doris.*;
import com.hllxd.hism.star.basic.service.model.metric.MetrichChartEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiyong.lxy
 * @date 2022-03-04 16:51
 */
@Slf4j
@Service("reportService")
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private KanbanFlowSummaryCubeMapper kanbanFlowSummaryCubeMapper;

    @Autowired
    private DorisReportMapper dorisReportMapper;


    @Override
    public List<FengkongEntity> listFengkong(String dt0, String dt1, String biz) {
        log.info("params: {}, {}.", dt0, dt1);
        return reportMapper.listFengkong(dt0, dt1, biz);
    }

    @Override
    public List<FlowSummaryBuserEntity> listCustomerReport(String bizTime, String biz, String adEtpname) {
        log.info("params: {}, {}, {}", bizTime, biz, adEtpname);
        List<FlowSummaryBuserEntity> ret = dorisReportMapper.listCustomerReport(bizTime, biz, adEtpname);
        log.info("size: {}", ret.size());
        return ret;
    }



    @Override
    public List<FlowSummaryEtpEntity> listEtpReport(String bizTime, String biz, String adEtpname) {
        log.info("params: {}, {}.", bizTime, biz);
        return dorisReportMapper.listEtpReport(bizTime, biz, adEtpname);
    }



    @Override
    public List<KanbanFlowSummaryFlow> selectSummaryFlow(KanbanFlowSummaryFlow param) {
        log.info("selectSummaryFlow: {}", JSON.toJSONString(param));
        return kanbanFlowSummaryCubeMapper.selectSummaryFlow(param);
    }

    @Override
    public List<FlowSummaryUserEntity> listUserReport(String dt, String biz, String imei) {
        log.info("params: {}, {}.", dt, biz);
        return dorisReportMapper.listUserReport(dt, biz, imei);
    }

    @Override
    public List<FlowSummaryAdEntity> listAdReport(String dt, String biz, String adId) {
        log.info("params: {}, {}.", dt, biz);
        return dorisReportMapper.listAdReport(dt, biz, adId);
    }

    @Override
    public List<KanbanFlowSummaryCube> listSummaryReport(KanbanFlowSummaryCube param) {
        //return kanbanFlowSummaryCubeMapper.selectByCondition(param);
        if(param.platform==null || param.platform.equals("0")) {
            param.platform = null;
        }
        List<KanbanFlowSummaryCube> ret = null;
        String topic = param.topicId;
        switch (topic) {
            case "140":// 企业，根据企业名称读取对应摊销收入
                log.info("dt:[{},{}],topic:{}, dimension:[{}], biz:{}, platform:{}, ent_name:{}, is_930:{}.",
                        param.dt,param.dt2, param.topicId, param.dimension, param.biz,param.platform,param.infoEtpname, param.isNewcate3);
                ret = kanbanFlowSummaryCubeMapper.select4Ent(param);
                break;
            case "231":// 客户
                ret = kanbanFlowSummaryCubeMapper.select4Buid(param);
                break;
            case "207": // 职位
                ret = kanbanFlowSummaryCubeMapper.select4Info(param);
                break;
            case "100":// 频道
                ret = kanbanFlowSummaryCubeMapper.select4Channel(param);
                break;
            default:
                log.warn("topic is not defined. {}", topic);
                break;
        }
        return ret;
    }

    @Override
    public MetrichChartEntity getCustomerChart(String metricCol, HashMap<String, String> dimMap, String tableName) {
        log.info("params: {}, {}.", dimMap, tableName);

        StringBuilder sb = new StringBuilder();
        dimMap.forEach((k, v) -> {
            if (v != null) {
                sb.append(" AND ").append(k).append("=").append("'").append(v).append("' ");
            }
        });

        // 100天
        String startDt = LocalDate.now().minusDays(100).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String endDt = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        HashMap<String, String> conditionMap = new HashMap<>();
        conditionMap.put("dimCondition", sb.toString());
        conditionMap.put("metricCol", metricCol);
        conditionMap.put("startDt", startDt);
        conditionMap.put("endDt", endDt);
        conditionMap.put("tableName", tableName);
        List<Map<String, Object>> res = kanbanFlowSummaryCubeMapper.listCommonReport(conditionMap);
        List<List<Object>> rows = new ArrayList<>();
        for (Map<String, Object> map : res) {
            List<Object> row = new ArrayList<>();
            String dt = map.get("dt").toString();
            Object metric_value = map.get(metricCol);
            row.add(dt);
            row.add(metric_value);
            rows.add(row);
        }
        MetrichChartEntity entity = new MetrichChartEntity();
        entity.setChartData(rows);
        return entity;
    }

    @Override
    public MetrichChartEntity getSummaryReportEntChart(String columnName, String tableName, HashMap<String, String> filterMap) {
        log.info("params: {}, {}.", filterMap, tableName);

        StringBuilder sb = new StringBuilder();
        filterMap.forEach((k, v) -> {
            if (v != null) {
                sb.append(" AND ").append(k).append("=").append("'").append(v).append("' ");
            }
        });
        String filterCondition = sb.toString();
        // 100天
        String dt = LocalDate.now().minusDays(100).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String dt2 = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

        log.info("query: {}, {}, {}.", tableName, columnName, filterCondition);
        List<Map<String, Object>> res = kanbanFlowSummaryCubeMapper.getSummaryReportEntChart(columnName, tableName, filterCondition, dt,dt2);
        List<List<Object>> rows = new ArrayList<>();
        for (Map<String, Object> map : res) {
            List<Object> row = new ArrayList<>();
            row.add(map.get("dt").toString());
            row.add(map.get(columnName));
            rows.add(row);
        }
        MetrichChartEntity entity = new MetrichChartEntity();
        entity.setChartData(rows);
        return entity;
    }


}
